﻿using System.Globalization;
using System.Windows.Data;
using DimensionsHelper.Common.Contracts.Excel;
using HAlign = System.Windows.HorizontalAlignment;
using VAlign = System.Windows.VerticalAlignment;

namespace DimensionsHelper.Client.Converters;

public class AlignConverter : IValueConverter
{

    public static AlignConverter Instance => new();


    public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return ConvertValue(value);
    }


    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return ConvertValue(value);
    }


    private static object ConvertValue(object? value)
    {
        return value switch
        {
            ExcelHAlign horizontalAlignment => ConvertToHAlign(horizontalAlignment),
            ExcelVAlign verticalAlignment => ConvertToVAlign(verticalAlignment),
            HAlign hAlign => ConvertToExcelHAlign(hAlign),
            VAlign vAlign => ConvertToExcelVAlign(vAlign),
            _ => $"{value} is not an alignment enum.",
        };
    }


    private static ExcelHAlign ConvertToExcelHAlign(HAlign hAlign)
    {
        return hAlign switch
        {
            HAlign.Center => ExcelHAlign.Center,
            HAlign.Right => ExcelHAlign.Right,
            _ => ExcelHAlign.Left,
        };
    }


    private static ExcelVAlign ConvertToExcelVAlign(VAlign vAlign)
    {
        return vAlign switch
        {
            VAlign.Center => ExcelVAlign.Center,
            VAlign.Bottom => ExcelVAlign.Bottom,
            _ => ExcelVAlign.Top
        };
    }


    private static HAlign ConvertToHAlign(ExcelHAlign hAlign)
    {
        return hAlign switch
        {
            ExcelHAlign.Center => HAlign.Center,
            ExcelHAlign.Right => HAlign.Right,
            _ => HAlign.Left,
        };
    }


    private static VAlign ConvertToVAlign(ExcelVAlign vAlign)
    {
        return vAlign switch
        {
            ExcelVAlign.Center => VAlign.Center,
            ExcelVAlign.Bottom => VAlign.Bottom,
            _ => VAlign.Top
        };
    }

}
